﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>

#include<string>

using namespace std;


#if 0
////////////////////////继承
class Person
{
protected:
	void Print()
	{
		cout << "name:" << _name << endl;
		cout << "age:" << _age << endl;
	}
protected:
	string _name = "peter"; // 姓名
	int _age = 18; // 年龄
};

class Student : public Person
{
public:
	void fun()//受保护成员在派生类里面可以调用（但是在类外面不可以调用）
	{
		_name = "haha";
		Print();//而对于私有成员只能在定义的类里使用，不能在派生类中使用	
	}
protected:
	int _stuid; // 学号
};
class Teacher : public Person
{
protected:
	int _jobid; // 工号
};
void test1()
{
	Student s;
	Teacher t;
	s.fun();

	//t.Print();

	Person p = s;//只有pubic继承才行
	Person& pp = s;
	//pubic继承，父子类是is-a的关系，子类对象赋值给父类是天然的，不会产生临时对象
	//父子类赋值兼容规则（切割/切片）

}

//////////////继承中的作用域
class person
{
protected:
	string _name = "小李子"; // 姓名
	int _num = 111; // 身份证号
};
class student : public person
{
public:
	void print()
	{
		cout << " 姓名:" << _name << endl;
		cout << " 身份证号:" << person::_num << endl;//派生类对基类同名成员的访问(隐藏同名成员)
		cout << " 学号:" << _num << endl;
	}
protected:
	int _num = 999; // 学号
};
void Test()
{
	student s1;
	s1.print();
};


/////////////////派生类的默认成员函数
class P
{
public:
	P(const char* name = "peter")
		: _name(name)
	{
		cout << "Person()" << endl;
	}
	P(const P& p)
		: _name(p._name)
	{
		cout << "Person(const Person& p)" << endl;
	}
	P& operator=(const P& p)
	{
		cout << "Person operator=(const Person& p)" << endl;
		if (this != &p)
			_name = p._name;
		return *this;
	}
	~P()
	{
		cout << "~Person()" << endl;
	}
protected:
	string _name; // 姓名
};
class S : public P
{
public:
	S(const char* name, int num)
		:P(name)	//派生类的构造函数分成两部分来看待，初始化父类的时
					//要将其看成完整的对象去初始化，不能挨个成员初始化 
		, _num(num)
	{
		cout << "Student()" << endl;
	}
	S(const S& s)
		:_num(s._num)
		,P(s)//子类可以天然的直接赋值给父类（切割）
	{
		cout << "S(const S& s)" << endl;
	}
	const S& operator=(const S& s)
	{
		this->_name = s._name;
		//this->operator=(s);//死循环，默认就近调用(同名成员函数构成隐藏关系)
		P::operator=(s);//指定调用
		return *this;
	}
	~S()//子类析构结束自动调用父类析构函数（防止父类先析构导致子调用父成员的非法）
	{
		//~P();//由于多态的原因，析构函数被统一处理成destructor，父子类析构函数构成隐藏
		cout << "~S()" << endl;
	}
	 
protected:
	int _num; //学号
};


void test2()
{
	S s("cr", 123);
	S s1 = s;

	S s2("某人", 250);
	s = s2;
}

/// /////////////////////////////////

class A
{
public:
	A()
	{}

	void f() { cout << "A::f()" << endl; }
	int a;
	static int aa;

};
int A::aa = 1;

class B : public A
{
public:
	B()
		:A()//显示调用构造函数
	{}

	void f(int a) { cout << "B::f()" << endl;}

	int b;
};



int main()
{
	B b;
	b.A::f();//调用A类的成员
	//b.f(2);//调用B类成员
	return 0;
}

////不能被继承的方式
//final关键字，构造设私有，友元不被继承,static静态成员是父子类共有一份

int sum = 0;
class Student;
class Person
{
	friend void Display(const Person& p, const Student& s);
public:
	Person() { sum++; }
	string _name="chenrui"; // 姓名
};
class Student : public Person
{
public:
	Student fun()
	{
		Student st;
		return st;
	}
	int _stuNum; // 学号
};
void Display(const Person& p, const Student& s)//友元函数
{
	cout << p._name << endl;
	//cout << s._stuNum << endl;
}
void main()
{
	Person p;//对象不能调用友元函数
	Student s;
	s.fun();
	Display(p, s);
}

//举例
class Person
{
public:
	string _name; // 姓名
};
class Student : virtual public Person
{
protected:
	int _num; //学号
};
class Teacher : virtual public Person
{
protected:
	int _id; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected:
	string _majorCourse; // 主修课程
};

int main()
{
	Assistant a;
	a.Student::_name = "陈同学";

	return 0;
}

//虚拟继承
class A
{
public:
	int _a;
};
class B:virtual public A
{
public:
	int _b;
};
class C:virtual public A
{
public:
	int _c;
};
class D :public B, public C
{
public:
	int _d;
};
int main()
{
	//D d;
	//
	//d._a = 0;
	//d._b = 3;
	//d._c = 4;
	//d._d = 5;
	B b;
	b._a = 1;
	b._b=2;

	return 0;
}

//例题
class Base1 { public: int _b1; };
class Base2 { public: int _b2; };
class Derive : public Base1, public Base2 //先继承的先创建
{
	public: int _d; 
};

int main() {
	Derive d;
	Base1* p1 = &d;
	Base2* p2 = &d;
	Derive* p3 = &d;
	return 0;
}

#endif

//例题
class A {
public:
	A(const char* s)
	{ cout << s << endl; }
	~A() {}
};
class B :virtual public A
{
public:
	B(const char* s1, const char* s2)
		:A(s1) 
	{ cout << s2 << endl; }
};
class C :virtual public A
{
public:
	C(const char* s1, const char* s2) 
		:A(s1) 
	{ cout << s2 << endl; }
};
class D :public B, public C
{
public:
	D(const char* s1, const char* s2, const char* s3, const char* s4) 
		:B(s1, s2), C(s1, s3), A(s1)
	{
		cout << s4 << endl; 
	}
};
int main() 
{
	D* p = new D("class A", "class B", "class C", "class D");
	delete p;
	return 0;
}

//class cr
//{
//public:
//	cr()
//		:_a(1)//初始化列表初始化的顺序是依据类成员声明的顺序
//		, _b(2)
//	{
//
//	}
//
//	int _b;
//	int _a;
//};
//int main()
//{
//	cr c;
//}